﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Locadora.TOB;
namespace Locadora.BLL.DAL
{
    class DvdDAL
    {
        internal static List<TB_DVD> Listar()
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var result = from c in dc.TB_DVDs where c.FL_LOCADO==false select c;
            return result.ToList<TB_DVD>();
        }

        internal static TB_DVD Listar(decimal? id)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            TB_DVD result = (from c in dc.TB_DVDs where c.CO_SEQ_DVD == id select c).First();
            return result;
        }
        internal static TB_DVD Listar(string nome)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            TB_DVD result = (from c in dc.TB_DVDs where c.DS_TITULO == nome select c).First();
            return result;
        }
        internal static void Update(TB_DVD tb)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            TB_DVD result = (from c in dc.TB_DVDs where c.CO_SEQ_DVD == tb.CO_SEQ_DVD select c).First();
            result.DS_ATOR_PRINCIPAL = tb.DS_ATOR_PRINCIPAL;
            result.DS_DIRETOR = tb.DS_DIRETOR;
            result.DS_TITULO = tb.DS_TITULO;
            result.NU_DURACAO = tb.NU_DURACAO;
            result.NU_NOTA = tb.NU_NOTA;
            result.CO_TIPO_DVD = tb.CO_TIPO_DVD;
            result.CO_GENERO = tb.CO_GENERO;

            dc.SubmitChanges();
            dc.Dispose();
        }

        internal static void UpdateFlagLocacao(TB_DVD tb)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            TB_DVD result = (from c in dc.TB_DVDs where c.CO_SEQ_DVD == tb.CO_SEQ_DVD select c).First();
            result.FL_LOCADO = tb.FL_LOCADO;
            dc.SubmitChanges();
            dc.Dispose();
        }

        internal static void Inserir(TB_DVD m_tb_dvd)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            dc.TB_DVDs.InsertOnSubmit(m_tb_dvd);
            dc.SubmitChanges();
            dc.Dispose();
        }

        internal static Dictionary<string, int> ListarTotalLocacoes()
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var result = from c in dc.TB_DVDs
                         join l in dc.TB_LOCACAOs on c.CO_SEQ_DVD equals l.CO_DVD
                         group c by c.DS_TITULO into d
                         select new
                         {
                              DVD = d.Key,
                              Locacoes = d.Count()
                         };
            Dictionary<string, int> resultado = result.ToDictionary(d => d.DVD, d => d.Locacoes);
            return resultado;
        }


        internal static List<TB_DVD> ListarByCoUsuario(string coUsuario)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var result = from c in dc.TB_DVDs 
                         join l in dc.TB_LOCACAOs on c.CO_SEQ_DVD equals l.CO_DVD
                         where (l.ST_LOCACAO != (int)Enumeracao.SituacaoLocacao.Devolvido && l.CO_USUARIO == coUsuario)
                         select c;
            foreach (var dvd in result)
            {
                dvd.TB_LOCACAOs.Load();
            }

            return result.ToList<TB_DVD>();
        }

        internal static void Devolver(TB_DVD tb)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            TB_DVD result = (from c in dc.TB_DVDs where c.CO_SEQ_DVD == tb.CO_SEQ_DVD select c).First();
            result.FL_LOCADO = tb.FL_LOCADO;
            result.NU_NOTA = tb.NU_NOTA;
            result.NU_QUANTIDADE = tb.NU_QUANTIDADE;
            dc.SubmitChanges();
            dc.Dispose();
        }

        internal static int GetTotalDvds()
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            return dc.TB_DVDs.Count();
        }

        internal static List<TB_DVD> GetDvds(int start, int offset)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var users = dc.TB_DVDs.Skip(start).Take(offset);
            return users.ToList();
        }

        internal static void UpdateFlagLocacao(decimal id, bool p)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            TB_DVD result = (from c in dc.TB_DVDs where c.CO_SEQ_DVD == id select c).First();
            result.FL_LOCADO = p;
            dc.SubmitChanges();
            dc.Dispose();
        }



       
    }
}
